﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using iMathMvc4.ModelViews;

namespace iMathMvc4.Controllers
{
    public class ExamController : Controller
    {
        private vn.chuyentin.service.iUserService userService = new vn.chuyentin.service.iUserService();
        private vn.chuyentin.imathservice.iMathService mathService = new vn.chuyentin.imathservice.iMathService();

        //
        // GET: /Exam/

        public ActionResult Index()
        {            
            var _exams = mathService.GetExams();
            return View("Index", _exams);
        }

        //
        // GET: /Exam/Details/5

        public ActionResult Details(int id)
        {
            //khởi tạo ExamView từ ModelView của iMathMvc4
            ExamView _examView = new ExamView();
            var _exam = mathService.GetExam(id);
            _examView.Id = _exam.Id;
            _examView.Name = _exam.Name;
            _examView.Description = _exam.Description;

            //Khởi tạo danh sách QuestionView từ Web Service
            List<vn.chuyentin.imathservice.QuestionView> _questions = new List<vn.chuyentin.imathservice.QuestionView>();
            _questions = mathService.GetQuestionsByExam(_exam.Id).ToList();

            if(_questions != null)
            {
                foreach (var _question in _questions)
                {
                    //khởi tạo đối tượng QuestionView từ Model của iMathMvc4
                    QuestionView _qView = new QuestionView();
                    _qView.Id = _question.Id;
                    _qView.Title = _question.Title;
                    _qView.Content = _question.Content;

                    _examView.Questions.Add(_qView);
                }
            }      
            return View("Details", _examView);
        }

        //
        // GET: /Exam/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Exam/Create
        private bool IsUserValid(string username, string password)
        {
            if (userService.Login(username, password))
                return true;
            else
                return false;
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(FormCollection collection)
        {
            string strStartDate = collection["StartDate"].ToString();
                DateTime StartDate = DateTime.Now;
                string strEndDate = collection["EndDate"].ToString();
                DateTime EndDate = DateTime.Now;

                try
                {
                    string Name = collection["Name"].ToString();
                    string Description = collection["Description"].ToString();

                    if (strStartDate.Length > 1)
                        StartDate = DateTime.Parse(strStartDate);
                    if (strEndDate.Length > 1)
                        EndDate = DateTime.Parse(strEndDate);

                    var _exam = mathService.PostExam("taipm", "P@$$w0rd", Name, Description, StartDate, EndDate);
                    if (_exam != null)
                        return RedirectToAction("Details", _exam);
                    else
                        return RedirectToAction("Error");
                }
                catch
                {
                    return View();
                }
        }

        //
        // GET: /Exam/Edit/5

        public ActionResult Edit(int id)
        {
            ExamView _examview = new ExamView();
            var _exam = mathService.GetExam(id);
            _examview.Id = _exam.Id;
            _examview.Name = _exam.Name;
            _examview.Description = _exam.Description;

            List<vn.chuyentin.imathservice.QuestionView> _questions = new List<vn.chuyentin.imathservice.QuestionView>();
            _questions = mathService.GetQuestionsByExam(_exam.Id).ToList();
            if (_questions != null)
            {
                foreach (var _question in _questions)
                {
                    QuestionView qView = new QuestionView();
                    qView.Id = _question.Id;
                    qView.Title = _question.Title;
                    qView.Content = _question.Content;

                    _examview.Questions.Add(qView);
                }
            }
            return View("Edit",_examview);
        }

        //
        // POST: /Exam/Edit/5

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(int id,FormCollection collection)
        {
            try
            {
                //string Name = collection["Name"].ToString();
                //string Description = collection["Description"].ToString();
                //string Title = collection["Title"].ToString();
                //string Content = collection["Content"].ToString();

                //var _exam = mathService.PostExam("taipm", "P@$$w0rd", Name, Description, DateTime.Now, DateTime.Now);
                //var _question = mathService.PostQuestionWithExam("taipm", "P@$$w0rd", Title, Content, id);
                //if (_question != null && _exam!=null)
                //{
                //    return RedirectToAction("Index", _exam);
                //}
               return RedirectToAction("Error");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Exam/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Exam/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
